#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入:
import os
from sqlalchemy import create_engine, Column, Integer, String, Text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()

# 定义对象:


class Score(Base):
    # 表的名字:
    __tablename__ = 'score'

    # 表的结构:
    score_id = Column(Integer, primary_key=True, autoincrement=True)
    stu_no = Column(Integer)
    course_id = Column(Integer)
    score = Column(Integer)


class Course(Base):
    # 表的名字:
    __tablename__ = 'course'

    # 表的结构:
    course_id = Column(Integer, primary_key=True, autoincrement=True)
    course_name = Column(String(200))


class Stu(Base):
    # 表的名字:
    __tablename__ = 'stu'

    # 表的结构:
    stu_no = Column(Integer, primary_key=True)
    stu_name = Column(String(200))


# 初始化数据库连接:
DB_HOST = os.getenv('DB_HOST')
DB_PASSWORD = os.getenv('DB_PASSWORD')
if DB_HOST is None:
    DB_HOST = 'localhost:3306'
if DB_PASSWORD is None:
    DB_PASSWORD = ''
print(f'mysql+pymysql://root:{DB_PASSWORD}@{DB_HOST}/score')
engine = create_engine(f'mysql+pymysql://root:{DB_PASSWORD}@{DB_HOST}/score')
# engine = create_engine('sqlite:///./test.db', echo=True)
# 创建表结构
Base.metadata.create_all(engine)
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)